﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-17>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：银行接口
//********************************************************************************//

using System;
using System.Data;
using System.IO;
using System.Text;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ShouFeiGL
{
    public partial class YinHangJK : BasePage
    {
        protected void gvGridTab3_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadGridDate3();
        }

        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtGridTab1
        {
            get { return ViewState["dtGridTab1"] as DataTable; }
            set { ViewState["dtGridTab1"] = value; }
        }

        /// <summary>
        ///     保存结果集2
        /// </summary>
        private DataTable dtGridTab2
        {
            get { return ViewState["dtGridTab2"] as DataTable; }
            set { ViewState["dtGridTab2"] = value; }
        }

        /// <summary>
        ///     保存结果集2_2
        /// </summary>
        private DataTable dtGridTab22
        {
            get { return ViewState["dtGridTab22"] as DataTable; }
            set { ViewState["dtGridTab22"] = value; }
        }

        /// <summary>
        ///     保存结果集3
        /// </summary>
        private DataTable dtGridTab3
        {
            get { return ViewState["dtGridTab3"] as DataTable; }
            set { ViewState["dtGridTab3"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _SpHelper = new SpHelper();

        /// <summary>
        ///     QS_SF_YINHANGPLDLHTH
        /// </summary>
        private readonly QS_SF_YINHANGPLDLHTHBLL YHHTHBLL = new QS_SF_YINHANGPLDLHTHBLL();

        /// <summary>
        /// </summary>
        private readonly QS_SF_YINHANGPLDLHZBLL YHHZBLL = new QS_SF_YINHANGPLDLHZBLL();

        /// <summary>
        /// </summary>
        private readonly QS_SF_YHPLDLHPHZBLL YHPLDLHPHZBLL = new QS_SF_YHPLDLHPHZBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                LoadGridDate2();
                // LoadGridDate3();
            }

            LoadGridDate1();
            ScriptManager1.RegisterPostBackControl(btnShengChengWJ);
            gvGrid1Tab2.DataSource = dtGridTab2;
            gvGrid1Tab2.DataBind();
            gvGrid2Tab2.DataSource = dtGridTab22;
            gvGrid2Tab2.DataBind();
            LoadGridDate3();
        }

        /// <summary>
        ///     生成文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnShengChengWJ_Click(object sender, EventArgs e)
        {
            //生成文件存储过程  P_QSSF_SHENGCHENGWJ

            var id = Convert.ToInt32(gvGrid1Tab2.GetRowValues(gvGrid1Tab2.FocusedRowIndex, "ID"));
            if (id == 0)
            {
                PageTools.AlertMsg(this, "请选择要生成的文件！");
                return;
            }

            var dt_detail = YHHZBLL.SelectQS_SF_YINHANGPLDLHZsDynamic(string.Format("ID={0}", id), null);
            var wenjian = dt_detail.Rows[0]["s_wenjianm"].ToString();

            var bh = Convert.ToInt32(dt_detail.Rows[0]["I_BIANHAO"]);
            var res = "";
            //生成文件操作
            var dt = _SpHelper.P_QSSF_SHENGCHENGWJ(wenjian).Tables[0];
            _SpHelper.P_QSSF_WENJIANJK(bh.ToString(), 0, out res);
            if (res != null)
            {
                PageTools.AlertMsg(this, res);
            }
            else
            {
                if (wenjian.Contains("sjjq")) wenjian = "sjjq" + DateTime.Now.ToString("yyyyMMdd");
                wenjian = wenjian + ".txt";
                PageTools.CreateTxt(dt, wenjian);
                LoadGridDate2();
            }
        }

        /// <summary>
        ///     取消文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuXiaoWJ_Click(object sender, EventArgs e)
        {
            //取消文件 存储过程 P_QSSF_WENJIANJK
            var id = Convert.ToInt32(gvGrid2Tab2.GetRowValues(gvGrid2Tab2.FocusedRowIndex, "ID"));
            var dt_detail = YHHZBLL.SelectQS_SF_YINHANGPLDLHZsDynamic(string.Format("ID={0}", id), null);
            var wenjian = dt_detail.Rows[0]["s_wenjianm"].ToString();
            var bh = Convert.ToInt32(dt_detail.Rows[0]["I_BIANHAO"]);
            var res = "";
            //取消文件操作

            _SpHelper.P_QSSF_WENJIANJK(bh.ToString(), 1, out res);
            if (res != null)
            {
                PageTools.AlertMsg(this, res);
            }
            else
            {
                PageTools.AlertMsg(this, "取消成功");
                LoadGridDate2();
            }
        }

        /// <summary>
        ///     导入文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UpLoadFile_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            var userid = LoginInfo.CurrentUser.UserID;
            var wtype = 2;
            var UploadDirectory = "/UPLOAD/YHJK_DXS/";
            if (UpLoadFile.UploadedFiles.Length == 0)
            {
                e.CallbackData = "";
                return;
            }

            var fileName = Path.GetFileName(UpLoadFile.UploadedFiles[0].FileName); //文件名
            var filePath = Path.Combine(Server.MapPath(UploadDirectory), fileName); //文件完整物理路径
            if (!Directory.Exists(Server.MapPath(UploadDirectory)))
                Directory.CreateDirectory(Server.MapPath(UploadDirectory));
            UpLoadFile.UploadedFiles[0].SaveAs(filePath);
            //写进数据库表
            //QS_SF_YHPLDLHPHZ
            //StreamReader read = new StreamReader(filePath, Encoding.Default);
            // string filetxt = File.ReadAllText(filePath, Encoding.Default);
            var filetxt2 = File.ReadAllLines(filePath, Encoding.Default);
            var RowsNumber = 0; //有数据的行数
            var arrI = new string[10];


            for (var i = 0; i < filetxt2.Length; i++) //i为读取的行数，包括没数据的行
                if (filetxt2[i] != "")
                {
                    if (i == 0)
                    {
                        arrI = filetxt2[i].Split(new[] {" ", " "}, StringSplitOptions.RemoveEmptyEntries);
                        _SpHelper.P_QS_INSERT_YHJK_HP(RowsNumber, fileName, "", arrI[0], arrI[1], arrI[2], "", "", "",
                            "", "", "",
                            Convert.ToDecimal(arrI[3]), Convert.ToInt32(arrI[4]), Convert.ToDecimal(arrI[5]),
                            Convert.ToInt32(arrI[6]));
                    }
                    else
                    {
                        arrI = filetxt2[i].Split(new[] {" ", " "}, StringSplitOptions.RemoveEmptyEntries);
                        if (arrI.Length == 10)
                            _SpHelper.P_QS_INSERT_YHJK_HP(RowsNumber, fileName, arrI[0], arrI[1], arrI[2], arrI[3],
                                arrI[4], arrI[5], arrI[6], arrI[7], arrI[8], arrI[9],
                                0, 0, 0, 0);
                    }

                    RowsNumber++;
                }

            var slym = BusinessTool.GetSLYMorZWNY(1, 2);
            var zwym = BusinessTool.GetSLYMorZWNY(2, 2);

            //将文件数据写入数据库
            var dt = _SpHelper.P_QS_YHJK_HPSJDR(2, zwym, slym, userid).Tables[0];
            var res = dt.Rows[0][0].ToString();

            //银行批量代理数据导入
            e.CallbackData = string.Join(",", fileName, UpLoadFile.UploadedFiles[0].ContentLength, res);
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = 2; // BusinessTool.WType;
            // gvGridTab1.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //gvGrid1Tab2.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //gvGrid2Tab2.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //gvGridTab3.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            var water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();

            //业务员
            //LoadYWY();
            txtShuiLiangYM.Text = water_y_m;
        }

        /// <summary>
        ///     加载tab1数据
        /// </summary>
        private void LoadGridDate1()
        {
            if (txtShuiLiangYM.Text.Trim() != "")
            {
                var ym = Convert.ToInt32(txtShuiLiangYM.Text.Trim());
                dtGridTab1 =
                    YHHTHBLL.SelectQS_SF_YINHANGPLDLHTHsDynamic(string.Format("i_jlzt=0 and i_ym='{0}'", ym), null);
                gvGridTab1.DataSource = dtGridTab1;
                gvGridTab1.DataBind();
            }
            else
            {
                gvGridTab1.DataSource = null;
                gvGridTab1.DataBind();
            }
        }

        /// <summary>
        ///     grid1刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvGridTab1_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            var ym = Convert.ToInt32(txtShuiLiangYM.Text.Trim());
            dtGridTab1 =
                YHHTHBLL.SelectQS_SF_YINHANGPLDLHTHsDynamic(string.Format("i_jlzt=0 and i_ym='{0}'", ym), null);
            gvGridTab1.DataSource = dtGridTab1;
            gvGridTab1.DataBind();
        }

        /// <summary>
        ///     加载tab2数据
        /// </summary>
        private void LoadGridDate2()
        {
            var wtype = 2; // BusinessTool.WType;
            dtGridTab2 =
                YHHZBLL.SelectQS_SF_YINHANGPLDLHZsDynamic(
                    string.Format("i_jlzt=0 and I_SHANGCHUANBZ=0 and I_WTYPE={0}", wtype), "I_BIANHAO");
            gvGrid1Tab2.DataSource = dtGridTab2;
            gvGrid1Tab2.DataBind();
            gvGrid1Tab2.FocusedRowIndex = 0;
            dtGridTab22 =
                YHHZBLL.SelectQS_SF_YINHANGPLDLHZsDynamic(
                    string.Format("i_jlzt=0 and I_SHANGCHUANBZ=1 and I_WTYPE={0}", wtype), "I_BIANHAO DESC");
            gvGrid2Tab2.DataSource = dtGridTab22;
            gvGrid2Tab2.DataBind();
            gvGrid2Tab2.FocusedRowIndex = 0;
        }

        /// <summary>
        ///     加载tab3数据
        /// </summary>
        private void LoadGridDate3()
        {
            dtGridTab3 = YHPLDLHPHZBLL.SelectQS_SF_YHPLDLHPHZsDynamic("i_jlzt=0", "D_DAORURQ desc");
            gvGridTab3.DataSource = dtGridTab3;
            gvGridTab3.DataBind();
        }

        #endregion
    }
}